/**
 * <p>ext辅助类</p>
 * xtype = cceafexthelper

 * @module Ext-ux-cceaf-util
 * @class cceaf.util.ExtHelper
 * @extends Object
 * @static
 * @since 1.0
 */
Ext.ns('cceaf.util');

Ext.define('cceaf.util.ExtHelper',{
	//extjs cmd只能通过类查找，需定义空类
});

cceaf.util.ExtHelper = (function() {
	
	return {
		
		/**
		 * 显示信息提示框
		 * 
		 * @param {String} message 显示信息
		 * @param {String} url 显示信息后导航的URL
		 * @return
		 */
		showInfoDialog: function(message,url,deferMillis){
			jumpurl = url;
			if(message != null && message != ""){
				Ext.MessageBox.show({
					title:'信息提示框',
					modal: true,
					msg: message,
					minWidth: 250,
					minHeight: 160,
					buttons: Ext.MessageBox.OK,
					icon: Ext.MessageBox.INFO,
					fn:function(){
						//定位到相关页面
						if(jumpurl != null && jumpurl != ""){
							if (jumpurl.indexOf("Jump2Logon.jsp") > -1) {
								window.location.href = jumpurl;
							} else {
								cceaf.util.CceafHelper.openUrl(jumpurl);
							}
						}
					}
				});
			}else{
				//Ext.MessageBox.hide();
				
				//定位到相关页面
				if(jumpurl != null && jumpurl != ""){
					if (jumpurl.indexOf("Jump2Logon.jsp") > -1) {
						window.location.href = jumpurl;
					} else {
						if (jumpurl.indexOf("Index.jsp") > -1) {
							window.location.href = jumpurl;
						} else {
							cceaf.util.CceafHelper.openUrl(jumpurl);
						}
					}
				}
			}
			
			//启动自动关闭定时器
			if (deferMillis) {
				Ext.Function.defer(this.hideWaitMsg, deferMillis);
			}
		},

		/**
		 * 显示错误提示框
		 * 
		 * @param {String} message 显示信息
		 * @param {String} url 显示信息后导航的URL
		 * @return
		 */
		showErrorDialog: function(message,url,deferMillis){
			jumpurl = url;
			if(message != null && message != ""){
				Ext.MessageBox.show({
					title:'错误提示框',
					modal: true,
					msg: message,
					minWidth: 250,
					minHeight: 160,
					buttons: Ext.MessageBox.OK,
					icon: Ext.MessageBox.ERROR,
					fn:function(){
						//定位到相关页面
						if(jumpurl != null && jumpurl != ""){
							if (jumpurl.indexOf("Jump2Logon.jsp") > -1) {
								window.location.href = jumpurl;
							} else {
								cceaf.util.CceafHelper.openUrl(jumpurl);
							}
						}
					}
				});
			}else{
				//Ext.MessageBox.hide();
				
				//定位到相关页面
				if(jumpurl != null && jumpurl != ""){
					if (jumpurl.indexOf("Jump2Logon.jsp") > -1) {
						window.location.href = jumpurl;
					} else {
						cceaf.util.CceafHelper.openUrl(jumpurl);
					}
				}
			}
			
			//启动自动关闭定时器
			if (deferMillis) {
				Ext.Function.defer(this.hideWaitMsg, deferMillis);
			}
		},

		/**
		 * 显示请求等待进度条窗口
		 * 
		 * @param {String} msg 显示信息
		 * @return
		 */
		showWaitMsg: function(msg) {
			Ext.MessageBox.show({
				title : '系统提示',
				msg : msg == null ? '正在处理数据,请稍候...' : msg,
				progressText : 'processing now,please wait...',
				width : 300,
				wait : true,
				waitConfig : {
					interval : 150
				}
			});
		},

		/**
		 * 隐藏请求等待进度条窗口
		 * 
		 * @return
		 */
		hideWaitMsg: function() {
			if (!Ext.MessageBox.isHidden()) {
				Ext.MessageBox.hide();
			}
		},

		/**
		 * 处理Ext.Ajax.request的success
		 * 
		 * @param {Object} response ajax返回的response
		 * @param {Boolean} issuccess ajax调用是否成功
		 * @return {Boolean} 是否成功处理
		 */
		processAjaxRequest: function(response,issuccess){
			var isProcessSucess = false;
			//隐藏进度条
			//Ext.MessageBox.hide();
			
			if(issuccess){
				var responseText = response.responseText; 

				if (responseText.substring(0, 6) == "<html>" ) {
					new Ext.Window({
		            	title: '错误提示',
		            	modal: true,
		            	width: 800,
		            	height: 600,
		            	autoScroll: true,
		            	//closeAction: 'hide',
		            	layout: 'fit',
		            	plain: true,
		            	html: responseText
		        	}).show();
		        	
		        	isProcessSucess = false;
				} else {
					var jsonObject =eval("(" + response.responseText + ")");  	
					
					if(jsonObject.success){
						this.showInfoDialog(jsonObject.message,jsonObject.url);
					}
					else{
						this.showErrorDialog(jsonObject.message,jsonObject.url);
					}
					
					isProcessSucess = jsonObject.success;
				}
			}
			else{
				if(response.status == 0){
					this.showErrorDialog('服务器处理超时');
				}
				else{
					var message = response.responseText; 
					this.showErrorDialog(message);
				}
			}

			return isProcessSucess;
		},
		
		/**
		 * 得到指定长度的HTML 空格（&nbsp;）
		 * 
		 * @param {Number} len html空格长度
		 * @return {Number} 指定长度的空格
		 */
		getHtmlSpace: function(len) {
			var retString = "";
			var htmlSpace = "&nbsp;";
			
			for(var i=0;i<len;i++){
				retString += htmlSpace;
			}
			
			return retString;
		},

		/**
		 * 填充HTML 空格（&nbsp;）
		 * 
		 *  @param {String} str: 待填充的字符串
		 *  @param {Number} totalLen: 填充区总长度，默认为6
		 *  @param {Number} fillLen: 待填充空格的长度
		 *  @param {Number} type: 0-左边填空，1-右边填空
		 *  @return {String} 填充空格后的字符窜
		 */
		fillHtmlSpace: function(str,totalLen,fillLen,type) {
			var retString = "";
			var spaceStr = "";
			var htmlSpace = "&nbsp;";
			
			if(!totalLen){
				totalLen = 6;
			}
			
			if(!fillLen){
				if (Ext.isIE) {
					fillLen = 4*(6-str.length);
				} else {
					fillLen = 3*(6-str.length);
				}
			}
			
			for(var i=0;i<fillLen;i++){
				spaceStr += htmlSpace;
			}
			
			var retString
			if(type){
				if(type == 0){
					retString = spaceStr + str;
				} else{
					retString = str + spaceStr;
				}
			} else{
				retString = spaceStr + str;
			}
			return retString;
		},
		
		/**
		 * 获取当前的浏览器名称
		 * 
		 * @return {String} 浏览器名称
		 */
		getBrowserName: function() {
			var browser;
			if (Ext.isChrome) {
				browser = "Chrome";
			} else if (Ext.isIE){
				browser = "IE";
			} else if (Ext.isOpera){
				browser = "Opera";
			} else if (Ext.isSafari){
				browser = "Safari";
			} else {
				if (Ext.firefoxVersion >0) {
					browser = "FireFox";
				} else {
					browser = "未知";
				}
			} 
			
			return browser;
		},
		
		/**
		 * 得到body的宽度
		 */
		getBodyWidth: function() {
			var width = document.body.clientWidth;
			if (width == null || width == 0) {
				width = document.documentElement.clientWidth;
			}
//			alert(document.body.clientWidth);
//			alert(document.body.clientHeight);
//			
//			alert(document.documentElement.clientWidth);
//			alert(document.documentElement.clientHeight);
			//alert(width);
			return width;
		},
		
		/**
		 * 得到body的高度
		 */
		getBodyHeight: function() {
			var height = document.body.clientHeight;
			if (height == null || height == 0) {
				height = document.documentElement.clientHeight;
			}
			//alert(height);
			return height;
		},
		
		/**
		 * 得到mainContent的宽度
		 */
		getMainContentWidth: function() {
			var mainContent = Ext.getCmp('mainContent');
			var width = 0;
			if (mainContent) {
				width = mainContent.getWidth();
			} else {
				width = this.getBodyWidth();
			}
			
			return width;
		},
		
		/**
		 * 得到mainContent的高度
		 */
		getMainContentHeight: function() {
			var mainContent = Ext.getCmp('mainContent');
			var height = 0;
			if (mainContent) {
				height = mainContent.getHeight();
			} else {
				height = this.getBodyHeight();
			}
			
			return height;
		},
		
		/**
		 * 处理Ext.Ajax.request的success
		 * 
		 * @param {action} 请求返回的action
		 */
		processFailureAction: function(action){
			var errorMessage;
			if (action.failureType === Ext.form.action.Action.CONNECT_FAILURE) {
				errorMessage = 'Status:'+action.response.status+': '+ action.response.statusText 
	     				+ "<br>通信错误"
	     				+ "<br>可能是设置了错误的action";
	        } else if (action.failureType === Ext.form.action.Action.SERVER_INVALID){
	        	errorMessage = "服务端验证错误<br>" + action.result.errormsg;
	        } else if (action.failureType === Ext.form.action.Action.CLIENT_INVALID){
	        	errorMessage = "客户端验证错误<br>";
	        } else if (action.failureType === Ext.form.action.Action.LOAD_FAILURE){
	        	errorMessage = "加载数据时，没有包含data属性的字段被返回<br>";
	        } else {
	        	errorMessage = "其他未知错误<br>";
	        }
	        
			cceaf.util.ExtHelper.showErrorDialog(errorMessage);
			
	     	//if(action.result == null) return;
	     	//cceaf.util.ExtHelper.showErrorDialog(action.result.message);   
		}
		
	};
	
}());

